import axios from 'axios'


class Http {
    constructor() {
        this.instance = axios.create({
            baseURL: import.meta.env.VITE_BASE_URL,
            timeout: 10000,
        })
        this.instance.interceptors.request.use(
            config => {
                // 在发送请求之前做些什么，比如添加 token
                return config
            },
            error => {
                // 对请求错误做些什么
                return Promise.reject(error)
            }
        )
        this.instance.interceptors.response.use(
            response => {
                // 对响应数据做点什么
                return response
            },
            error => {
                // 对响应错误做点什么
                return Promise.reject(error)
            }
        )
    }
    post(url, data) {
        return new Promise(async(resolve, reject) => {
            try {
                const response = await this.instance.post(url, data)
                resolve(response.data)
            } catch (error) {
                try {
                    const detail = error.response.data.detail;
                    reject(detail)
                } catch (error) {
                    reject('服务器错误！')
                }
            }
        })
    }
    get(url,parmas) {
        return new Promise(async(resolve, reject) => {
            try {
                const response = await this.instance.get(url,{params:parmas})
                resolve(response.data)
            } catch (error) {
                try {
                    const detail = error.response.data.detail;
                    reject(detail)
                } catch (error) {
                    reject('服务器错误！')
                }
            }
        })
    }
    put(url,data) {
        return new Promise(async(resolve, reject) => {
            try {
                const response = await this.instance.put(url,data)
                resolve(response.data)
            } catch (error) {
                try {
                    const detail = error.response.data.detail;
                    reject(detail)
                } catch (error) {
                    reject('服务器错误！')
                }
            }
        })
    }
    delete(url) {
        return new Promise(async(resolve, reject) => {
            try {
                const response = await this.instance.delete(url)
                resolve(response.data)
            } catch (error) {
                try {
                    const detail = error.response.data.detail;
                    reject(detail)
                } catch (error) {
                    reject('服务器错误！')
                }
            }
        })
    }
    downloadFile(url,parmas){
        return new Promise(async(resolve, reject) => {
            try {
                const response = await this.instance.get(url,{params:parmas,responseType:'blob'})
                resolve(response)
            } catch (error) {
                try {
                    const detail = error.response.data.detail;
                    reject(detail)
                } catch (error) {
                    reject('服务器错误！')
                }
            }
        })
    }
}
export default new Http()